package com.ruoyi.web.controller.mate;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.MatePurRequisition;
import com.ruoyi.domain.ProImpl;
import com.ruoyi.domain.ThisStock;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IMatePurRequisitionService;
import com.ruoyi.service.IThisStockService;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用料采购申请Controller
 *
 * @author ruoyi
 * @date 2020-07-06
 */
@Controller
@RequestMapping("/ruoyi/requisitionVerify")
public class MatePurRequisitionVerifyController extends BaseController {
    private String prefix = "ruoyi/requisition";

    @Autowired
    private IMatePurRequisitionService matePurRequisitionService;
    @Resource
    private IThisStockService thisStockService;


    @RequiresPermissions("ruoyi:requisitionVerify:view")
    @GetMapping()
    public String requisition() {
        return prefix + "/requisitionVerify";
    }

    /**
     * 查询用料采购申请列表
     */
    @RequiresPermissions("ruoyi:requisition:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Long sid) {
        startPage();
        List<MatePurRequisition> list = matePurRequisitionService.selectMatePurRequisitionLists(sid);
        list.removeIf(m -> m.getmStatus() == 0);
        return getDataTable(list);
    }

    /**
     * 导出用料采购申请列表
     */
    @RequiresPermissions("ruoyi:requisition:export")
    @Log(title = "用料采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MatePurRequisition matePurRequisition) {
        List<MatePurRequisition> list = matePurRequisitionService.selectMatePurRequisitionList(matePurRequisition);
        ExcelUtil<MatePurRequisition> util = new ExcelUtil<MatePurRequisition>(MatePurRequisition.class);
        return util.exportExcel(list, "requisition");
    }

    /**
     * 新增用料采购申请
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存用料采购申请
     */
    @RequiresPermissions("ruoyi:requisition:add")
    @Log(title = "用料采购申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MatePurRequisition matePurRequisition) {
        return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//        ThisStock thisStock = new ThisStock();
//        //获取用户输入的id
//        long a = matePurRequisition.getmId();
//        //获取用户选择的材料
//        String name = matePurRequisition.getmName();
//        //查询本厂库存有没有用户用的材料
//        ThisStock thisStock1 = thisStockService.selectThisStockByIds(a, name);
//        //当审批通过时
//        if (matePurRequisition.getmStatus() ==0){
//            return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//        }else if (matePurRequisition.getmStatus() == 2) {
//            //如果返回的对象是空的就代表本厂库存没有所以就向本厂库存添加相应的材料和数量
//            if (null == thisStock1) {
//                thisStock.settId(matePurRequisition.getmId());
//                thisStock.settNum(matePurRequisition.getmNum());
//                thisStock.settName(matePurRequisition.getmName());
//                thisStockService.insertThisStock(thisStock);
//                return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//            //如果用户选择的材料本厂库存里有,就只更新材料的数量
//            } else if (matePurRequisition.getmName().equals(thisStock1.gettName())) {
//                long num = (thisStock1.gettNum() + matePurRequisition.getmNum());
//                thisStockService.updateThisStocks(num, name);
//                return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//            } else {
//                return toAjax(matePurRequisitionService.insertMatePurRequisition(matePurRequisition));
//            }
//        }
//        return null;
    }

    /**
     * 删除用料采购申请
     */
    @RequiresPermissions("ruoyi:requisition:remove")
    @Log(title = "用料采购申请", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(matePurRequisitionService.deleteMatePurRequisitionByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        int leave = matePurRequisitionService.submitExaStatus(id);

        return success();
    }

    /**
     * 撤销申请
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping("/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        int leave = matePurRequisitionService.revoExaStatus(id);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }

    /**
     * 通过申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/consent")
    @ResponseBody
    public AjaxResult consent(Long id) {
        matePurRequisitionService.conExaStatus(id);
        //通过id查询对应的材料对象
        MatePurRequisition matePurRequisition = matePurRequisitionService.ExaId(id);
        //获取到申请通过的材料名称
        String name = matePurRequisition.getmName();
        //获取到申请通过的材料数量
        Long num = matePurRequisition.getmNum();
        //获取到申请通过材料所在的总项目
        Long mid = matePurRequisition.getmId();
        //查询本厂库存整个表(每个项目下的)
        List<ThisStock> thisStockList = thisStockService.selectThisStockLists(mid);
        //单独提出name字段成为一个集合
        List<String> list = thisStockList.stream().map(ThisStock::gettName).collect(Collectors.toList());
        //判断是否存在  不存在返回-1
        int i = list.indexOf(name);
        //如果是-1 就代表集合当中没有这个元素
        if (-1 !=i) {
            //如果本厂库存里有这个材料就查询出id
            ThisStock thisStock = thisStockService.selectThisStockByIds(mid, name);
            //数据库原有的材料数量
            Long gettNumnum = thisStock.gettNum();
            //只添加材料的数量 申请数量+原有数量
            Long anum = num + gettNumnum;
            return toAjax(thisStockService.updateThisStocks(anum, name));
        } else {
            ThisStock thisStock = new ThisStock();
            thisStock.settName(name);
            thisStock.settNum(num);
            thisStock.settId(mid);
            return toAjax(thisStockService.insertThisStock(thisStock));
        }
    }

        /**
         * 驳回【审批】
         */
        @GetMapping("/edit/{id}")
        public String edit (@PathVariable("id") Long id, ModelMap mmap)
        {
            matePurRequisitionService.editExaStatus(id);
            MatePurRequisition matePurRequisition = matePurRequisitionService.selectMatePurRequisitionById(id);
            mmap.put("matePurRequisition", matePurRequisition);
            return prefix + "/Verifyedit";
        }

        /**
         * 驳回保存【审批】
         */
        @RequiresPermissions("ruoyi:requisition:edit")
        @Log(title = "审批", businessType = BusinessType.UPDATE)
        @PostMapping("/edit")
        @ResponseBody
        public AjaxResult confirm (MatePurRequisition matePurRequisition){
            return toAjax(matePurRequisitionService.updateMatePurRequisition(matePurRequisition));
        }

    }
